package com.qfjy.controller.example6;

import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;

@Controller
@RequestMapping("user")
@Slf4j
public class UserController {
    /**
     * 消息队列
     */
    @Autowired
    private AmqpTemplate amqpTemplate;

    /**
     * 注册功能
     *
     * @return
     */
    @ResponseBody
    @GetMapping("register")
    public String register(@RequestParam("uname") String uname, @RequestParam("time") int time) {
        // 延迟的时间毫秒 ms
        int delayTime = time;
        log.info("用户业务逻辑注册成功--》" + uname + "\t" + new Date().toString() + "\t" + delayTime + "毫秒");

        //队列发送 邮件  延迟 delayTime
        amqpTemplate.convertAndSend("delayed.exchange.user.register",
                "delayed.key.user.register", uname, msg -> {
                    msg.getMessageProperties().setDelay(delayTime);
                    return msg;
                });
        return "用户注册成功";
    }


}